package com.ytf.test.seventh;

class Insect{
    private int k = printInit("Insect.k initialized");
    private int i =9;
    protected int j ;

    public Insect() {
        System.out.println("i="+i+", j="+j);
        j=39;
    }
    private static int x1=printInit("static Insect.x1 initialized");
    static int printInit(String s){
        System.out.println(s);
        return 47;
    }
    {
        System.out.println("Insect.j"+j);
    }
    static {
        System.out.println("static {} Insect");
    }
    public static void main(String[] args) {
        System.out.println("Insect.main");
    }
}
/**
 * @author: YTF
 * @date: 2024/12/4 15:37
 * @version: 1.0
 * @description:
 */
public class Beetle extends Insect {
    private int k = printInit("Beetle.k initialized");
    public Beetle() {
        System.out.println("k="+k);
        System.out.println("j="+j);
    }

    static {
        System.out.println("static {} Beetle 1");
    }
    private static int x2 = printInit("static Beetle.x2 initialized");

    {
        System.out.println("Beetle.j"+j);
    }
    static {
        System.out.println("static {} Beetle 2");
    }
    public static void main(String[] args) {
        System.out.println("Beetle constructor");
        Beetle b = new Beetle();
        /**
         * Output:
         * static Insect.x1 initialized
         * static {} Insect
         * static {} Beetle 1
         * static Beetle.x2 initialized
         * static {} Beetle 2
         * Beetle constructor
         * Insect.k initialized
         * Insect.j0
         * i=9, j=0
         * Beetle.k initialized
         * Beetle.j39
         * k=47
         * j=39
         *
         * 总结：
         * 执行顺序为
         * 在Beetle上执行Java时，先试图访问Beetle.main()，于是加载器开始启动找出Beetle类的编译代码（在名为Beetle.class的文件之中）.
         * 在加载过程中，编译器注意到它有一个基类（这是由关键字extends得知的），于是它进行加载。
         * 我的理解是
         * 先加载父类static再加载子类 static. -> 访问类的main方法 -> 父类构造器再子类构造器
         * static的加载顺序和类中排序有关
         *
         */
    }
}
